package com.onetech.adrewards.free.fragments.v2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.analytics.tracking.android.EasyTracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.BaseNewActivity;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.config.AppConfig.Cache;
import com.onetech.adrewards.free.dialogs.LoginBonusDialog;
import com.onetech.adrewards.free.dialogs.ReviewAppSuccessDialog;
import com.onetech.adrewards.free.fragments.ExchangeFragmentNew;
import com.onetech.adrewards.free.fragments.GetPointsFragmentNewApi;
import com.onetech.adrewards.free.fragments.ListNewsFragment;
import com.onetech.adrewards.free.utils.AdrewadsUtils;

public class HomeFragment extends BaseFragment {
	public ViewPager mViewPager = null;
	private FragmentPagerAdapter mAdapter = null;
	public boolean isReviewApp = false;
	public String reviewSuccessMessage = "";

	private static final String[] CONTENT = new String[] { "Appli",
			"Recommend", "Free" };
	private View mView = null;

	private View menuBottomLeft = null;
	private View menuBottomMiddle = null;
	private View menuBottomRight = null;

	private View mFooterView = null;

	public int currentView = R.id.menuMiddle;
	private Fragment mGetPointsFragment = null;
	private Fragment mExchangeFragment = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		startNotifyAppLaunch();
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_home_layout, container,
					false);
			initView();
			initListener();

			if (AppConfig.LOGIN_BONUS) {
				LoginBonusDialog mLoginBonusDialog = new LoginBonusDialog(
						getActivity());
				mLoginBonusDialog.show();
			}

		} else {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}

	private void startNotifyAppLaunch() {
		AdrewadsUtils.checkUId();
	}

	private void initView() {
		menuBottomLeft = (LinearLayout) mView.findViewById(R.id.menuLeft);
		menuBottomMiddle = (LinearLayout) mView.findViewById(R.id.menuMiddle);
		menuBottomRight = (LinearLayout) mView.findViewById(R.id.menuRight);

		mAdapter = new MyFragementAdapter(getChildFragmentManager());
		mViewPager = (ViewPager) mView.findViewById(R.id.pager);
		mViewPager.setAdapter(mAdapter);
		currentView = R.id.menuMiddle;
		setDefaultMenuBackground();

		mViewPager.setCurrentItem(1);
		switchLayout(currentView);
	}

	private void initListener() {
		mViewPager.setOnPageChangeListener(this);

		menuBottomLeft.setOnClickListener(this);
		menuBottomMiddle.setOnClickListener(this);
		menuBottomRight.setOnClickListener(this);
	}

	private void setDefaultMenuBackground() {
		menuBottomLeft
				.setBackgroundResource(R.drawable.menu_bottom_background_selected);
		menuBottomMiddle
				.setBackgroundResource(R.drawable.menu_bottom_background_normal);
		menuBottomRight
				.setBackgroundResource(R.drawable.menu_bottom_background_normal);
	}

	public void switchLayout(int id) {
		switch (id) {
		case R.id.menuLeft:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			break;
		case R.id.menuMiddle:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			break;
		case R.id.menuRight:
			menuBottomLeft
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomMiddle
					.setBackgroundResource(R.drawable.menu_bottom_background_normal);
			menuBottomRight
					.setBackgroundResource(R.drawable.menu_bottom_background_selected);
			break;
		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		// switchCurrentItemByViewId(currentView);
		switch (v.getId()) {
		case R.id.menuLeft:
			currentView = R.id.menuLeft;
			break;
		case R.id.menuMiddle:
			currentView = R.id.menuMiddle;
			break;
		case R.id.menuRight:
			currentView = R.id.menuRight;
			break;
		default:
			super.onClick(v);
			break;
		}
		switchLayout(currentView);
		switchCurrentItemByViewId(currentView);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		switchCurrentItem(position);
		switchLayout(currentView);
	}

	public void switchToTabView(int bottomTabIndex, int tabIndex) {
		switchCurrentItem(bottomTabIndex);
		switch (bottomTabIndex) {
		case 1:
			switchGetPointsTabView(tabIndex);
			break;
		case 2:
			switchExchangeTabView(tabIndex);
			break;
		default:
			break;
		}		
	}

	private void switchCurrentItemByViewId(int resId) {
		switch (resId) {
		case R.id.menuLeft:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.menuMiddle:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.menuRight:
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}

	public void switchCurrentItem(int position) {
		switch (position) {
		case 0:
			currentView = R.id.menuLeft;
			mViewPager.setCurrentItem(0);
			break;
		case 1:
			currentView = R.id.menuMiddle;
			mViewPager.setCurrentItem(1);
			break;
		case 2:
			currentView = R.id.menuRight;
			mViewPager.setCurrentItem(2);
			break;
		default:
			break;
		}
	}

	class MyFragementAdapter extends FragmentPagerAdapter{
		public MyFragementAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return Fragment.instantiate(getActivity(),
						ListNewsFragment.class.getName());
			case 1:
				mGetPointsFragment = Fragment.instantiate(getActivity(),
						GetPointsFragmentNewApi.class.getName());
				return mGetPointsFragment;
			case 2:
				mExchangeFragment=Fragment.instantiate(getActivity(),
						ExchangeFragmentNew.class.getName());
				return mExchangeFragment;
			default:
				return Fragment.instantiate(getActivity(),
						ListNewsFragment.class.getName());
			}
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return CONTENT[position % CONTENT.length].toUpperCase();
		}		
		@Override
		public int getCount() {
			return CONTENT.length;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		getParentActivity().getUserInfo();
		checkReviewApp();
	}

	private BaseNewActivity getParentActivity() {
		return (BaseNewActivity) getActivity();
	}

	private void checkReviewApp() {
		if (isReviewApp) {
			isReviewApp = false;

			AdrewadsUtils.setStore(getActivity(), Cache.REVIEW_APP_KEY, "Done");
			if (reviewSuccessMessage != null
					&& reviewSuccessMessage.trim().length() > 0) {
				ReviewAppSuccessDialog dialog = new ReviewAppSuccessDialog(
						getActivity(), getString(R.string.app_name),
						reviewSuccessMessage);
				dialog.show();
			}
		}
	}

	public void loadUserInfo() {
		getParentActivity().getUserInfo();
	}

	@Override
	public void onStart() {
		super.onStart();
		// The rest of your onStart() code.
		try {
			EasyTracker.getInstance(getActivity()).activityStart(getActivity()); // Add
																					// this
			// method.
		} catch (Exception e) {
		}
	}

	@Override
	public void onStop() {
		super.onStop();
		// The rest of your onStop() code.
		try {
			EasyTracker.getInstance(getActivity()).activityStop(getActivity()); // Add
																				// this
			// method.
		} catch (Exception e) {
		}
	}

	public void switchGetPointsTabView(int topAdsTabIndex) {
		try {
			GetPointsFragmentNewApi mGetPointsFragmentNewApi = (GetPointsFragmentNewApi) mGetPointsFragment;
			if (mGetPointsFragmentNewApi != null) {
				mGetPointsFragmentNewApi.switchFragmentViewPager(topAdsTabIndex);
			}
		} catch (Exception e) {
			return;
		}
	}
	public void switchExchangeTabView(int tabIndex){
		try {
			ExchangeFragmentNew mExchangeFragmentNew=(ExchangeFragmentNew)mExchangeFragment;
			if(mExchangeFragmentNew!=null){
				mExchangeFragmentNew.menuSelected(tabIndex);
			}
		} catch (Exception e) {
			return;
		}
	}
}
